(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/Script/gameSwitch/startGame.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, '2d163uimghCkJMDma9gob5/', 'startGame', __filename);
// Script/gameSwitch/startGame.ts

Object.defineProperty(exports, "__esModule", { value: true });
var Constant_1 = require("../constant/Constant");
var NumberShow_1 = require("../number/NumberShow");
// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var EventSystem = /** @class */ (function (_super) {
    __extends(EventSystem, _super);
    function EventSystem() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.gameStartObj = null; //开始游戏界面
        _this.slider = null; //游戏物体
        _this.reStartObj = null; //结束游戏界面
        _this.bgNode = null; //背景图
        _this.scoreShow = null; //分数显示
        _this.scoreAdder = null; //积分器
        _this.faildGameScoreShow = null; //游戏失败分数计数器
        /**
         * 障碍块
         */
        _this.twoSideDog = null; //两边大障碍
        _this.middleDog = null; //中间大障碍
        _this.leftDog = null; //左边大障碍
        _this.rightDog = null; //右边大障碍
        _this.produceSpeed = 2.5;
        _this.gaming = false; //游戏进行标志
        _this.middArr = [];
        _this.leftBigArr = [];
        _this.rightBigArr = [];
        _this.twoSideArr = [];
        return _this;
        // private speed: number = 1;
        // update(dt) {
        //     this.changeSpeed(this.speed++);
        // }
    }
    /**
     * 随机生成飞机
     */
    EventSystem.prototype.randomGetPlane = function () {
        var t = this;
        var rand = parseInt((Math.random() * 4) + "");
        switch (rand) {
            case 0:
                t.activeOneOfArr(t.twoSideArr);
                break;
            case 1:
                t.activeOneOfArr(t.middArr);
                break;
            case 2:
                t.activeOneOfArr(t.rightBigArr);
                break;
            case 3:
                t.activeOneOfArr(t.leftBigArr);
                break;
            default:
                // t.twoSideDog.active = true;
                break;
        }
    };
    /**
     * 开始游戏按钮
     */
    EventSystem.prototype.startGame = function () {
        this.gameStartObj.active = false;
        this.reStartObj.active = false;
        this.enableAllGamingObj();
        this.gaming = true;
        Constant_1.Constant.NOW_SCORE = 0; //当前分数清零
    };
    /**
     * 结束游戏按钮
     */
    EventSystem.prototype.endGame = function () {
        cc.game.end();
    };
    /**
     * 游戏开始画面
     */
    EventSystem.prototype.mainMenu = function () {
        this.gameStartObj.active = true;
        this.disableAllGamingObj();
    };
    /**
     * 游戏结束画面
     */
    EventSystem.prototype.gameFailed = function () {
        this.gaming = false;
        this.reStartObj.active = true;
        var lastNum = Constant_1.Constant.NOW_SCORE;
        this.scoreShow.getChildByName("ScoLayer").getComponent(NumberShow_1.default).showNumber(0); //分数清零
        this.faildGameScoreShow.getComponent(NumberShow_1.default).showNumber(lastNum);
        this.disableAllGamingObj();
    };
    /**
     * 将所有游戏物体失效
     */
    EventSystem.prototype.disableAllGamingObj = function () {
        var t = this;
        Constant_1.Constant.GAME_SPEED = 20;
        t.slider.active = false;
        t.scoreShow.active = false;
        t.resetAll();
        cc.director.getCollisionManager().enabled = false; //关闭物理系统
        //cc.director.getCollisionManager().enabledDebugDraw = false;//关闭碰撞盒阴影
        cc.log("物理系统关闭");
    };
    /**
     * 将所有的游戏物体生效
     */
    EventSystem.prototype.enableAllGamingObj = function () {
        cc.director.getCollisionManager().enabled = true; //开启物理系统
        //  cc.director.getCollisionManager().enabledDebugDraw = true;//显示碰撞盒阴影
        cc.log("物理系统开启");
        this.slider.active = true;
        this.scoreShow.active = true;
    };
    /**
     * 改变游戏速度
     * @param speed
     */
    EventSystem.prototype.changeSpeed = function (speed) {
        Constant_1.Constant.GAME_SPEED = speed;
        this.produceSpeed = 50 / Constant_1.Constant.GAME_SPEED;
    };
    /**
     * 系统垃圾回收
     */
    EventSystem.prototype.gc = function () {
        cc.sys.garbageCollect();
        cc.log("垃圾回收完毕");
    };
    EventSystem.prototype.onLoad = function () {
    };
    /**
     * 重置所有的节点
     */
    EventSystem.prototype.resetAll = function () {
        this.resetAllNode(this.middArr);
        this.resetAllNode(this.leftBigArr);
        this.resetAllNode(this.rightBigArr);
        this.resetAllNode(this.twoSideArr);
    };
    //单独接口
    EventSystem.prototype.resetAllNode = function (arr) {
        arr.forEach(function (e) {
            e.y = Constant_1.Constant.SCREEN.height / 2;
            e.active = false;
        });
    };
    /**
     * 激活未被激活的节点
     * @param arr
     */
    EventSystem.prototype.activeOneOfArr = function (arr) {
        for (var index = 0; index < arr.length; index++) {
            if (arr[index].active)
                continue;
            arr[index].active = true;
            break;
        }
    };
    /**
     * 复制节点存入集合
     * @param temp
     * @param arr
     */
    EventSystem.prototype.fill = function (temp, arr) {
        var canvas = cc.find("Canvas");
        for (var i = 0; i < 5; i++) {
            var copy = cc.instantiate(temp);
            copy.parent = canvas;
            copy.active = false;
            arr.push(copy);
        }
        arr.push(temp);
    };
    EventSystem.prototype.start = function () {
        var t = this;
        //TODO:复制节点存入集合
        t.fill(t.leftDog, t.leftBigArr);
        t.fill(t.rightDog, t.rightBigArr);
        t.fill(t.middleDog, t.middArr);
        t.fill(t.twoSideDog, t.twoSideArr);
        //END
        t.gameStartObj.active = true;
        t.changeSpeed(Constant_1.Constant.GAME_SPEED);
        this.schedule(function () {
            if (t.gaming) {
                t.randomGetPlane();
            }
        }, t.produceSpeed);
        setInterval(function () {
            t.gc();
        }, 60000); //60s一次GC
        //TODO:后台切换监听
        cc.game.on(cc.game.EVENT_HIDE, function () {
            console.log("游戏进入后台");
            cc.game.pause();
        }, t);
        cc.game.on(cc.game.EVENT_SHOW, function () {
            console.log("重新返回游戏");
            cc.game.resume();
        }, t);
    };
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "gameStartObj", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "slider", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "reStartObj", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "bgNode", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "scoreShow", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "scoreAdder", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "faildGameScoreShow", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "twoSideDog", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "middleDog", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "leftDog", void 0);
    __decorate([
        property(cc.Node)
    ], EventSystem.prototype, "rightDog", void 0);
    EventSystem = __decorate([
        ccclass
    ], EventSystem);
    return EventSystem;
}(cc.Component));
exports.default = EventSystem;

cc._RF.pop();
        }
        if (CC_EDITOR) {
            __define(__module.exports, __require, __module);
        }
        else {
            cc.registerModuleFunc(__filename, function () {
                __define(__module.exports, __require, __module);
            });
        }
        })();
        //# sourceMappingURL=startGame.js.map
        